<?php

namespace zhtfrontend\modules\cardmanage\controllers;

use common\components\helpers\Fun;
use common\models\datas\Cards;
use common\models\datas\CardsBatch;
use common\models\datas\CardsIssuerinfor;
use yii\helpers\ArrayHelper;
use zhtfrontend\modules\cardmanage\components\rest\ActiveController;
use yii\web\Response;

class ReviewController extends ActiveController
{
    public $modelClass = 'common\models\datas\CardsBatch';

    public $serializer = [
        'class' => 'zhtfrontend\modules\cardmanage\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionList(){
        $modelClass = $this->modelClass;
        $data = \Yii::$app->request->get();
        $user_id = ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';
        $site=ArrayHelper::keyExists('site', $data) ? $data['site'] : 2;
        if($site=='qdlcaks'){
            $site=2;
        }
        $apply_person=CardsIssuerinfor::find()->where(['parent_id'=>$user_id])->asArray()->all();
        $apply_person_array=ArrayHelper::getColumn($apply_person, 'id');
        $card_review_list=$modelClass::find()->where(['applicant_id' => $apply_person_array, 'site_id' => $site,'is_delete'=>0])->all();
        $card_review_listn=[];
        foreach ($card_review_list as $k => $v) {
            switch ($v->status){
                case 0:
                    $v->status='审核中';break;
                case 1:
                    $v->status='通过';break;
                case 2:
                    $v->status='失败';break;
            };
            $card_review_listn[$k] = [
                'apply_id'=>$v->id,
                'applicant'=>isset($v->cardsIssuerinfor->name)?$v->cardsIssuerinfor->name:'',
                'apply_time'=>date('Y-m-d', $v->created_at),
                'title'=>$v->title,
                'status'=>$v->status,
            ];
        }
        return ['items'=>$card_review_listn];
    }

    public function actionReviewlist(){
        $modelClass = $this->modelClass;
        $data = \Yii::$app->request->get();
        $apply_id=ArrayHelper::keyExists('id', $data) ? $data['id'] : '';
        $site=ArrayHelper::keyExists('site', $data) ? $data['site'] : 2;
        if($site=='qdlcaks'){
            $site=2;
        }
        $card_apply_detail=$modelClass::find()->where(['id' => $apply_id, 'site_id' => $site,'is_delete'=>0])->one();
        switch ($card_apply_detail->status){
            case 0:
                $card_apply_detail->status='审核中';break;
            case 1:
                $card_apply_detail->status='通过';break;
            case 2:
                $card_apply_detail->status='失败';break;
        };
        switch ($card_apply_detail->card_type){
            case 0:
                $card_apply_detail->card_type='实体卡';break;
            case 1:
                $card_apply_detail->card_type='虚拟红包';break;
        };
        $card_apply_detail->created_at=date('Y-m-d', $card_apply_detail->created_at);
        $card_apply_detail->grant_time=date('Y-m-d', $card_apply_detail->grant_time);
        $card_apply_detail->applicant_id=isset($card_apply_detail->applicant_id)?$card_apply_detail->cardsIssuerinfor->name:'';
        //下面是需要查找开始得卡号得信息
        $cards=Cards::find()->where(['status'=>0])->asArray()->all();
        $cards_cardno=ArrayHelper::map($cards, 'id', 'card_no');
        $cards_review=[
            'card_apply_detail'=>$card_apply_detail,
            'cards_cardno'=>$cards_cardno
        ];
        return ['items'=>$cards_review];
    }
    public function actionReview(){
        $modelClass = $this->modelClass;
        $data = \Yii::$app->request->get();
        $id=ArrayHelper::keyExists('id', $data) ? $data['id'] :'';
        $approver_number=ArrayHelper::keyExists('cardno_number', $data) ? $data['cardno_number'] :'';
        $approver_id=ArrayHelper::keyExists('approver_id', $data) ? $data['approver_id'] :'';
        $cardno_begin=ArrayHelper::keyExists('cardno_begin', $data) ? $data['cardno_begin'] :'';
        $card_remark=ArrayHelper::keyExists('card_remark', $data) ? $data['card_remark'] :'';
        $status=ArrayHelper::keyExists('status', $data) ? $data['status'] :'';
        $site=ArrayHelper::keyExists('site', $data) ? $data['site'] : 2;
        if($site=='qdlcaks'){
            $site=2;
        }
        $cards=Cards::find()->where(['>=', 'id', $cardno_begin])->andWhere(['status'=>0])->limit($approver_number)->asArray()->all();
        $cards_begin=Cards::find()->where(['id'=>$cardno_begin])->one();
        //如果当前开始得卡后面没有那么多得卡需要进行提示
        if (count($cards)<$approver_number){
            $items=[
               'card_count'=>count($cards),
            ];
            return ['statusCode' =>201, 'message' => 'error', 'result' =>$items];
        }
        $model=$modelClass::find()->where(['id' => $id, 'site_id' => $site])->one();
        $model->approver_id=$approver_id;
        $model->approver_number=$approver_number;
        $model->approver_remark=$card_remark;
        $model->cardno_begin=isset($cards_begin->id)?$cards_begin->id:'';
        $model->cardno_end=isset($cards[$approver_number-1])?$cards[$approver_number-1]['id']:'';
        $model->grant_time=strtotime(date('Y-m-d'));
        $model->updated_at=strtotime(date('Y-m-d'));
        $model->status=$status;
        //下面是需要把选择好的卡存储到card_attr表里面去，批量保存
        if ($model->save(false)){
            //首先把已经领用的卡得状态改编为已经领用得状态
            foreach ($cards as $key=>$val){
                \Yii::$app->db->createCommand()->update('{{%cards}}', ['status' => 1])->execute();
                $insertArr[]=[
                    'site_id'=>$site,
                    'card_id'=>(int)$val['id'],
                    'batch_id'=>(int)$model->id,
                    'card_no'=>$val['card_no'],
                    'v_code'=>$val['v_code'],
                    'customer_id'=>0,
                    'active_time'=>0,
                    'issuser_id'=>(int)$model->applicant_id,
                    'reissuser_id'=>0,
                    'sendtype'=>(int)$model->card_type,
                    'integral'=>(int)$model->integral,
                    'cash'=>(int)$model->cash,
                    'created_at'=>strtotime(date('Y-m-d')),
                    'updated_at'=>strtotime(date('Y-m-d')),
                ];
            }
            foreach ($insertArr as $k=>$v){
                \Yii::$app->db->createCommand()->Insert('{{%cards_attr}}', $v)->execute();
            }
            return ['items'=>$model];
        }else{
            return ['statusCode' =>0, 'message' => 'error', 'result' =>'提交审批失败'];
        }
    }


    public function actionDetail(){
        $modelClass = $this->modelClass;
        $data = \Yii::$app->request->get();
        $review_id = ArrayHelper::keyExists('review_id', $data) ? $data['review_id'] : '';
        $site=ArrayHelper::keyExists('site', $data) ? $data['site'] : 2;
        if($site=='qdlcaks'){
            $site=2;
        }
        if ($review_id!=='' || isset($review_id)){
            $card_review_detail=$modelClass::find()->where(['id' => $review_id,'site_id' => $site,'is_delete'=>0])->one();
            switch ($card_review_detail->status){
                case 0:
                    $card_review_detail->status='审核中';break;
                case 1:
                    $card_review_detail->status='通过';break;
                case 2:
                    $card_review_detail->status='失败';break;
            };
            switch ($card_review_detail->card_type){
                case 0:
                    $card_review_detail->card_type='实体卡';break;
                case 1:
                    $card_review_detail->card_type='虚拟红包';break;
            };
            $cardno_begin=Cards::find()->where(['id'=>$card_review_detail->cardno_begin])->one();
            $cardno_end=Cards::find()->where(['id'=>$card_review_detail->cardno_end])->one();
            $card_review_detail->cardno_begin=isset($cardno_begin->card_no)?$cardno_begin->card_no:'';
            $card_review_detail->cardno_end=isset($cardno_end->card_no)?$cardno_end->card_no:'';
            $card_review_detail->created_at=date('Y-m-d', $card_review_detail->created_at);
            $card_review_detail->grant_time=date('Y-m-d', $card_review_detail->grant_time);
            $card_review_detail->applicant_id=isset($card_review_detail->applicant_id)?$card_review_detail->cardsIssuerinfor->name:'';
            $card_review_detail->approver_id=isset($card_review_detail->approver_id)?$card_review_detail->cardsIssuerinfor->name:'';
            return ['items'=>$card_review_detail];
        }else{
            return ['statusCode' =>0, 'message' => 'error', 'result' =>'审批信息不存在'];
        }
    }

}